package com.emellend.api11.media;


import java.io.File;

import com.emellend.api11.R;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
/**
 * 播放视频
 * @author tiangh
 *
 */
public class MediaPlayerDemo_Video extends Activity implements OnBufferingUpdateListener,OnCompletionListener,
	OnPreparedListener,OnVideoSizeChangedListener,SurfaceHolder.Callback{
	
	private static final String TAG = "MediaPlayerDemo";
    private int mVideoWidth;
    private int mVideoHeight;
    private MediaPlayer mMediaPlayer;
    private SurfaceView mPreview;
    private SurfaceHolder holder;
    private String path;
    private Bundle extras;
    private boolean mIsVideoSizeKnown = false;
    private boolean mIsVideoReadyToBePlayed = false;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.mediaplayer_2);
    	mPreview=(SurfaceView) findViewById(R.id.surface);
    	holder=mPreview.getHolder();
    	holder.addCallback(this);
    	extras=getIntent().getExtras();
    }
    @Override
    protected void onPause() {
    	super.onPause();
    	releaseMediaPlayer();
    	doCleanUp();
    }
	@Override
    protected void onDestroy() {
    	super.onDestroy();
    	releaseMediaPlayer();
        doCleanUp();
    }
    /**********************************************************************************************************/
    private void playVideo(Integer Media){
    	doCleanUp();
    	try {
			switch (Media) {
			case MediaPlayerDemo.LOCAL_VIDEO:
				//在这里设置本地文件路径
				initPah();
				if (path=="") {
					Toast.makeText(this, "应该设置本地视频文件路径", Toast.LENGTH_LONG).show();
				}
				break;
			case MediaPlayerDemo.STREAM_VIDEO:
				//在这里设置网络流媒体路径
				path="";
				if (path=="") {
					Toast.makeText(this, "设置正确的网络流媒体的Url链接，必须是mp4或者3gpp", Toast.LENGTH_LONG).show();
				}
				break;
			}
			mMediaPlayer=new MediaPlayer();
			mMediaPlayer.setDataSource(path);
			mMediaPlayer.setDisplay(holder);
			mMediaPlayer.prepare();
			mMediaPlayer.setOnBufferingUpdateListener(this);
			mMediaPlayer.setOnCompletionListener(this);
			mMediaPlayer.setOnPreparedListener(this);
			mMediaPlayer.setOnVideoSizeChangedListener(this);
			mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
		} catch (Exception e) {
			Log.e(TAG, "error: " + e.getMessage(), e);
		}
    }
    private void initPah() {
		if (Environment.getExternalStorageState()!=Environment.MEDIA_UNMOUNTED) {
			File file=new File(Environment.getExternalStorageDirectory(), "yanni 2006_Until the Last Moment.3gp");
			if (file.exists()&&file.isFile()) {
				path=file.getAbsolutePath();
			}
		}
	}
	private void doCleanUp() {
		mVideoWidth=0;
		mVideoHeight=0;
		mIsVideoReadyToBePlayed=false;
		mIsVideoSizeKnown=false;
	}
	private void startVideoPlayback() {
		Log.v(TAG, "startVideoPlayback");
        holder.setFixedSize(mVideoWidth, mVideoHeight);
        mMediaPlayer.start();
	}
	
	private void releaseMediaPlayer() {
		if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
	}
	/**********************************************************************************************************/
	public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
		Log.d(TAG, "surfaceChanged called");
	}
	public void surfaceCreated(SurfaceHolder holder) {
		 Log.d(TAG, "surfaceCreated called");
	     playVideo(extras.getInt(MediaPlayerDemo.MEDIA));
	}
	public void surfaceDestroyed(SurfaceHolder holder) {
		Log.d(TAG, "surfaceDestroyed called");
	}
	public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
		Log.v(TAG, "onVideoSizeChanged called");
		if (width==0||height==0) {
			Log.e(TAG, "invalid video width(" + width + ") or height(" + height + ")");
            return;
		}
		mIsVideoSizeKnown=true;
		mVideoWidth=width;
		mVideoHeight=height;
		if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
            startVideoPlayback();
        }
	}
	public void onPrepared(MediaPlayer mp) {
		Log.d(TAG, "onPrepared called");
		mIsVideoReadyToBePlayed=true;
		if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
            startVideoPlayback();
        }
	}
	public void onCompletion(MediaPlayer mp) {
		Log.d(TAG, "onCompletion called");
	}
	public void onBufferingUpdate(MediaPlayer mp, int percent) {
		Log.d(TAG, "onBufferingUpdate percent:" + percent);
	}
}
